/* @(#)GcgIncrementalStateButton.java
** 
** Copyright (C) 2012 by Steven D. Stamps
**
**             Trademarks & Copyrights
** Flywheel Management Science(TM), Flywheel Management Model(TM),
** Flywheel Story Editor(TM) and FlywheelMS(TM) are exclusive trademarks
** of Steven D. Stamps and may only be used freely for the purpose of
** identifying the unforked version of this software.  Subsequent forks
** may not use these trademarks.  All other rights are reserved.
**
** DecKanGL (Decorated Kanban Glyph Language) and TribKn (Tribal Knowledge)
** are also exclusive trademarks of Steven D. Stamps.  These may be used
** freely within the unforked FlywheelMS application and documentation.
** All other rights are reserved.
**
** gConGUI (game Controller Graphical User Interface) is an exclusive
** trademark of Steven D. Stamps.  This trademark may be used freely
** within the unforked FlywheelMS application and documentation.
** All other rights are reserved.
**
** Trademark information is available at
** <http://www.flywheelms.com/trademarks>
**
** Flywheel Management Science(TM) is a copyrighted body of management
** metaphors, governance processes, and leadership techniques that is
** owned by Steven D. Stamps.  These copyrighted materials may be freely
** used, without alteration, by the community (users and developers)
** surrounding this GPL3-licensed software.  Additional copyright
** information is available at <http://www.flywheelms.org/copyrights>
**
**              GPL3 Software License
** This program is free software: you can use it, redistribute it and/or
** modify it under the terms of the GNU General Public License, version 3,
** as published by the Free Software Foundation. This program is distributed
** in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
** even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
** PURPOSE.  See the GNU General Public License for more details. You should
** have received a copy of the GNU General Public License, in a file named
** COPYING, along with this program.  If you cannot find your copy, see
** <http://www.gnu.org/licenses/gpl-3.0.html>.
*/
package com.flywheelms.library.gcg.widget;

import com.flywheelms.library.gcg.helper.GcgHelper;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;

public abstract class GcgIncrementalStateButton extends Button {
	
	protected static boolean INCREMENT_STATE_ON_PERFORM_CLICK = true;
	protected int state;
	private static final int state__ON_FULL = 1;
	private int MAX_STATES;
	private int state__OFF;
	private int[] drawableResourceIdArray;
	private int[] descriptionStringResourceIdArray;

    public GcgIncrementalStateButton(Context aContext, int[] aDrawableResourceIdArray) {
        super(aContext);
        setupStateManagement(aDrawableResourceIdArray);
    }

	public GcgIncrementalStateButton(Context aContext, AttributeSet anAttributeSet,
			int[] aDrawableResourceIdArray) {
        super(aContext, anAttributeSet);
        setupStateManagement(aDrawableResourceIdArray);
	}

	public GcgIncrementalStateButton(Context aContext, AttributeSet anAttributeSet,
			int aDefStyle, int[] aDrawableResourceIdArray) {
        super(aContext, anAttributeSet, aDefStyle);
        setupStateManagement(aDrawableResourceIdArray);
	}

    public GcgIncrementalStateButton(Context aContext, AttributeSet anAttributeSet, int[] aDrawableResourceIdArray, int[] aDescriptionStringResourceIdArray) {
            super(aContext, anAttributeSet);
            setupStateManagement(aDrawableResourceIdArray);
            setupLongPress(aDescriptionStringResourceIdArray);
    }

	private void setupLongPress(int[] aDescriptionStringResourceIdArray) {
		if(aDescriptionStringResourceIdArray == null) {
			return;
		}
		this.descriptionStringResourceIdArray = aDescriptionStringResourceIdArray;
		if(this.descriptionStringResourceIdArray.length == this.drawableResourceIdArray.length) {
			// build popup
			// setOnLongClickListener
			setOnLongClickListener(new OnLongClickListener() {
				
				@Override
				public boolean onLongClick(View v) {
					// present pop-up and process selection
					GcgHelper.makeToast("Long press...");
					return true;
				}
			});
		}
		
	}

	private void setupStateManagement(int[] aDrawableResourceIdArray) {
		this.drawableResourceIdArray = aDrawableResourceIdArray;
		this.MAX_STATES = this.drawableResourceIdArray.length;
		this.state__OFF = this.MAX_STATES;
		this.state = GcgIncrementalStateButton.state__ON_FULL;
		this.setBackgroundDrawable();
	}
    
    @Override
    public boolean performClick() {
            if(INCREMENT_STATE_ON_PERFORM_CLICK) {
            	incrementState();
            }
            return super.performClick();
    }
    
    public boolean isOff() {
    	return this.state == this.state__OFF ? true : false;
    }
    
    public boolean isOn() {
    	return this.state == GcgIncrementalStateButton.state__ON_FULL ? true : false;
    }
    
    public void setState(int aState) {
            if((aState > 0) && (aState <= this.MAX_STATES)) {
                    this.state = aState;
                    setBackgroundDrawable();
            }
    }
    
    public int getState() {
            return this.state;
    }

    public void incrementState() {
            this.state++;
            if(this.state > this.MAX_STATES) {
                    this.state = GcgIncrementalStateButton.state__ON_FULL;
            }
            setBackgroundDrawable();
    }
    
    public void decrimentState() {
            this.state--;
            if(this.state < GcgIncrementalStateButton.state__ON_FULL) {
                    this.state = this.MAX_STATES;
            }
            setBackgroundDrawable();
    }
    
    public int getMaxStates() {
    	return this.MAX_STATES;
    }
    
    protected void setBackgroundDrawable() {
    	this.setBackgroundResource(this.drawableResourceIdArray[this.state - 1]);
    }

}
